package com.github.bitsapling.sapling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.bitsapling.sapling.mapper.TagMapper;
import com.github.bitsapling.sapling.entity.Tag;
import com.github.bitsapling.sapling.service.TagService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Locale;


@Service
public class TagServiceImpl implements TagService {
    @Resource
    private TagMapper tagMapper;

    @Override
    public Tag getTagByName(String tagName) {
        tagName = tagName.toLowerCase(Locale.ROOT);
        return tagMapper.selectOne(
                new QueryWrapper<Tag>().eq("name", tagName)
        );
    }

    @Override
    public Tag create(Tag tag) {
        // 执行插入操作
        int rows = tagMapper.insert(tag);
        if (rows > 0) {
            // 插入成功，通过主键查询插入后的完整实体
            return tagMapper.selectById(tag.getId());
        }
        // 插入失败，返回 null
        return null;
    }
}
